package api

import (
	"fmt"
	"mytest/initialize/database"
	"mytest/models"
	"mytest/models/request"
	"mytest/service/student"
	"mytest/service/system"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type UniversalResp struct {
	Code  int         `json:"code"`
	Data  interface{} `json:"data"`
	Msg   string      `json:"msg"`
	Count int64       `json:"count,omitempty"`
}

//接受参数，调用service接口
func CreateStu(c *gin.Context) {
	var form models.Student
	if e := c.Bind(&form); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())

		return
	}
	if form.Name == "" || form.Age == 0 {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  "请传入完整参数",
		})
	}
	db := database.GetDB()
	e := student.Create(db, &form)
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("插入失败: %v", e.Error()),
		})

		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Data: form,
	})
}
func DeleteStu(c *gin.Context) {
	id := c.Param("id")
	i, e := strconv.Atoi(id)
	if e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())

		return
	}
	db := database.GetDB()
	e = student.DeleteById(db, uint(i))
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("删除失败: %v", e.Error()),
		})

		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Data: i,
	})
}
func UpdateStu(c *gin.Context) {
	var form models.Student
	if e := c.Bind(&form); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())
		return
	}
	if form.ID == 0 {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  "请传入ID",
		})
		return
	}
	db := database.GetDB()
	e := student.Update(db, &form)
	if e != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("更新失败: %v", e.Error()),
		})

		return
	}

	c.JSON(http.StatusOK, UniversalResp{
		Data: form,
	})
}
func StudentList(c *gin.Context) {
	var req request.StudentList
	if e := c.Bind(&req); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())
		return
	}
	db := database.GetDB()
	err, out, count := student.GetStuList(db, req)
	if err != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("查询失败: %v", err.Error()),
		})

		return
	}
	c.JSON(http.StatusOK, UniversalResp{
		Data:  out,
		Count: count,
	})
}

func DownCsv(c *gin.Context) {
	var f request.DownByField
	if e := c.Bind(&f); e != nil {
		c.String(http.StatusBadRequest, "bad request: %v", e.Error())
		return
	}
	out, err := system.DownCsv(database.GetDB(), f)
	if err != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("失败: %v", err.Error()),
		})

		return
	}
	c.Writer.Header().Set("Content-type", "application/octet-stream") //设置文件格式
	c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "test.csv"))
	_, err = c.Writer.Write(out)
	if err != nil {
		c.JSON(http.StatusOK, UniversalResp{
			Code: 1,
			Msg:  fmt.Sprintf("失败: %v", err.Error()),
		})
		return
	}
}
